//
//  LXJHomeWeiboCell.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/30.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit

/// 自定义微博的 cell
class LXJHomeWeiboCell: UITableViewCell {
    /// 微博的模型属性,并在控件上显示内容
    var weibo: LXJWeiboViewModel? {
        didSet{
            /// 控件赋值
            originalWeibo.originalWeibo = weibo
            toolBar.weiboCount = weibo
            /// 工具条的位置动态调整
            toolBarTopConstraintFitForView(weibo: weibo)
        }
    }
    
    // MARK: 控件的懒加载
    /// 原创微博
    fileprivate lazy var originalWeibo: LXJOriginalWeiboView = LXJOriginalWeiboView()
    /// 工具条
    fileprivate lazy var toolBar: LXJToolBarView = LXJToolBarView()
    /// 转发微博
    fileprivate lazy var retweetedWeibo: LXJRetweetedWeiboView = LXJRetweetedWeiboView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        /// 界面搭建
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() {
        /// 设置背景颜色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        /// 添加控件
        contentView.addSubview(originalWeibo)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetedWeibo)
        
        /// 自动布局
        originalWeibo.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(homeWeiboCellmargin)
            make.leading.trailing.equalTo(contentView)
        }
        
        retweetedWeibo.snp.makeConstraints { (make) in
            make.top.equalTo(originalWeibo.snp.bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        toolBar.snp.makeConstraints { (make) in
            /// 约束属性赋值
            toolBarTopConstraint = make.top.equalTo(retweetedWeibo.snp.bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(36)
            make.bottom.equalTo(contentView)
        }
    }
    
    ///toolbar 距离上方的约束
    var toolBarTopConstraint: Constraint?
    
    fileprivate func toolBarTopConstraintFitForView(weibo: LXJWeiboViewModel?) -> Void {
        /// 卸载原来的约束条件
        toolBarTopConstraint?.deactivate()
        
        /// 有转发的微博
        if weibo?.homeWeiboList?.retweeted_status != nil {
            /// 显示转发微博
            retweetedWeibo.isHidden = false
            /// 控件赋值
            retweetedWeibo.retweetedWeibo = weibo
            /// 添加约束
            toolBar.snp.makeConstraints({ (make) in
                toolBarTopConstraint = make.top.equalTo(retweetedWeibo.snp.bottom).constraint
            })
        }else{/// 没有转发的微博
            /// 隐藏转发微博
            retweetedWeibo.isHidden = true
            /// 添加约束
            toolBar.snp.makeConstraints({ (make) in
                toolBarTopConstraint = make.top.equalTo(originalWeibo.snp.bottom).constraint
            })
        }
    }
}
